<?php

class MallController extends Controller
{

    /*
    * 获取一级菜单列表
    *
    */
    public function actionGetMaterialTypeList()
    {
        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //获取列表
            $searchsql = Util::initSearchSql();

            //获取list
            $list = MallMaterialType::model()->findAll($searchsql);


            //返回数据
            Util::responseList(count($list), $list);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");


            //添加
            $obj = new MallMaterialType();
            Util::initObjData($obj);


            $obj->name = $name;


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //验证是否存在子菜单
            $searchsql = Util::initSearchSql();
            $searchsql = Util::getSerarchStrAdd($searchsql,"type_id","$obj_id");
            $childcount = MallMaterial::model()->count($searchsql);

            if($childcount >= 1){
                throw new Exception("该一级菜单下有二级菜单存在，请先删除其下的二级菜单");
            }


            //删除
            $obj = MallMaterialType::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改
    *
    */
    public function actionUpdate()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $name = Util::getParamNotEmpty("name");


            //验证存在
            $obj = MallMaterialType::model()->findByPK($obj_id);

            Util::checkExistAndNotDelete($obj);

            //修改信息

            $obj->name = $name;

            //保存
            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取二级材料列表分页
    *
    */
    public function actionGetMaterialListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStr($searchsql,'type_id','type_id');

            //获取count
            $totalcount = MallMaterial::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = MallMaterial::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 添加
    *
    */
    public function actionAddMaterial()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");
            $type_id = Util::getParamNotEmpty("type_id");
            $pic_url = Util::getParamNotEmpty("pic_url");

            //验证传过来的type_id是否在种类表存在
            $type_obj = MallMaterialType::model()->findByPK($type_id);
            Util::checkExistAndNotDelete($type_obj);

            //添加
            $obj = new MallMaterial();
            Util::initObjData($obj);


            $obj->name = $name;
            $obj->type_id = $type_id;
            $obj->type_name = $type_obj->name;
            $obj->pic_url = $pic_url;


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改
    *
    */
    public function actionUpdateMaterial()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $name = Util::getParamNotEmpty("name");
            $type_id = Util::getParamNotEmpty("type_id");
            $pic_url = Util::getParamNotEmpty("pic_url");


            //验证传过来的type_id是否在种类表存在
            $type_obj = MallMaterialType::model()->findByPK($type_id);
            Util::checkExistAndNotDelete($type_obj);


            //验证存在
            $obj = MallMaterial::model()->findByPK($obj_id);

                Util::checkExistAndNotDelete($obj);

                //修改信息
                $obj->name = $name;
                $obj->type_id = $type_id;
                $obj->type_name = $type_obj->name;
                $obj->pic_url = $pic_url;

                //保存
                Util::saveObjData($obj);



                //返回数据
                Util::responseMsg(MsgConst::RETURN_SUCCESS);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDeleteMaterial()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = MallMaterial::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }



}